<?php
require_once APP_PATH . 'user/model.php';

function run_login_app($action = '') {

	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		//登录检查
		login_check_aciton();
	} else {
		//显示登录页面
		login_show_aciton();
	}

}
/**
 * 显示user页面
 * @param  string
 * @return [type]
 */
function login_show_aciton() {
	$action = isset($_POST['action']) ? trim($_POST['action']) : '';
	if (!empty($action) && $action == 'login') {
		login_check_aciton();
	} else {
		include APP_ADMIN_VIEW . '/login/login.php';
	}

}

function login_check_aciton() {
	$action = isset($_POST['action']) ? trim($_POST['action']) : '';
	if (!empty($action) && $action == 'login') {

		$account = isset($_POST['account']) ? trim($_POST['account']) : '';
		$password = isset($_POST['password']) ? intval($_POST['password']) : 0;
		require_once APP_PATH . DIRECTORY_SEPARATOR . 'user/model.php';
		$ret = user_model_login($account, $password);
		if ($ret) {
			//存放到session中去
			$_SESSION['is_login'] = true;
			$_SESSION['uid'] = $ret['id'];
			$_SESSION['nickname'] = $ret['nickname'];
			$_SESSION['username'] = $ret['username'];
			$_SESSION['account'] = $ret['username'];
			$_SESSION['login_code'] = 1;
			$_SESSION['login_msg'] = '登录成功';
			header("Location:/" . APP_ADMIN_URI . "/");
		} else {
			$_SESSION['login_code'] = 0;
			$_SESSION['login_msg'] = '账号或密码错误';
			header("Location:/" . APP_ADMIN_URI . "/login");
		}

	}
}